001 /* 002 * Copyright 2004 Niclas Hedhman. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 013 * implied. 014 * 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018 019 package net.dpml.transit.monitor; 020 021 import java.net.URL; 022 023 /** 024 * A router that handles multicasr distribution of monitor events to registered monitors. 025 * 026 * @author <a href="http://www.dpml.net">The Digital Product Meta Library</a> 027 * @version 1.0.0 028 */ 029 public class NetworkMonitorRouter extends AbstractMonitorRouter 030 implements NetworkMonitor, Router 031 { 032 033 //-------------------------------------------------------------------- 034 // NetworkMonitor 035 //-------------------------------------------------------------------- 036 037 /** 038 * Notify all subscribing monitors of a updated event. 039 * @param resource the url of the updated resource 040 * @param expected the size in bytes of the download 041 * @param count the progress in bytes 042 */ 043 public void notifyUpdate( URL resource, int expected, int count ) 044 { 045 Monitor[] monitors = getMonitors(); 046 for( int i=0; i < monitors.length; i++ ) 047 { 048 NetworkMonitor monitor = (NetworkMonitor) monitors[i]; 049 monitor.notifyUpdate( resource, expected, count ); 050 } 051 } 052 053 /** 054 * Notify all subscribing monitors of a download completion event. 055 * @param resource the url of the downloaded resource 056 */ 057 public void notifyCompletion( URL resource ) 058 { 059 Monitor[] monitors = getMonitors(); 060 for( int i=0; i < monitors.length; i++ ) 061 { 062 NetworkMonitor monitor = (NetworkMonitor) monitors[i]; 063 monitor.notifyCompletion( resource ); 064 } 065 } 066 067 /** 068 * Add a monitor to the list of monitors managed by this router. 069 * @param monitor the monitor to add 070 * @exception IllegalArgumentException if the supplied monitor is not a NetworkMonitor 071 */ 072 public void addMonitor( Monitor monitor ) throws IllegalArgumentException 073 { 074 if( !( monitor instanceof NetworkMonitor ) ) 075 { 076 throw new IllegalArgumentException( "monitor must be NetworkMonitor type." ); 077 } 078 else 079 { 080 super.addMonitor( monitor ); 081 } 082 } 083 } 084